package com.lanou.sfu.songforu.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.lanou.sfu.songforu.R;
import com.lanou.sfu.songforu.activity.MainActivity;
import com.lanou.sfu.songforu.adapter.RankClassAdapter;
import com.lanou.sfu.songforu.base.BaseFragment;
import com.lanou.sfu.songforu.datas.MusicListDatas;
import com.lanou.sfu.songforu.db.DaoSingleton;
import com.lanou.sfu.songforu.db.SongListEntity;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.media.QQShareContent;
import com.umeng.socialize.sso.QZoneSsoHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;

import java.util.ArrayList;

/**
 * Created by dllo on 15/11/5.
 */
public class CollectListFragment extends BaseFragment implements View.OnClickListener,RankClassAdapter.OnClickToShareLinstener{
    private View v;
    private ImageButton collect_back,play;
    private ListView listView;
    private DaoSingleton daoSingleton;
    private RankClassAdapter adapter;
    private Context context;
    private TextView total,offLine;
    private UMSocialService mController;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.fragment_collect_list, null);
        init();
        return v;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // Qzone分享
        QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler(getActivity(),getString(R.string.qQ_appID),getString(R.string.qQ_appSecret));
        qZoneSsoHandler.addToSocialSDK();

        // qq分享
        UMQQSsoHandler qqssoH = new UMQQSsoHandler(getActivity(), getString(R.string.qQ_appID),getString(R.string.qQ_appSecret));
        qqssoH.addToSocialSDK();

        // 设置分享内容
        QQShareContent qqcontent = new QQShareContent();
        qqcontent.setShareContent(getString(R.string.SHARE_CONTENT));
        mController.setShareMedia(qqcontent);
        collect_back.setOnClickListener(this);
        // 查询所有数据加入list
        final ArrayList<SongListEntity> allList = (ArrayList<SongListEntity>) daoSingleton.getSongListEntityDao().queryBuilder().list();
        adapter = new RankClassAdapter(context,allList);
        adapter.setOnClickToShareLinstener(CollectListFragment.this);
        listView.setAdapter(adapter);
        total.setText(allList.size() + "");

        play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ArrayList<MusicListDatas> lists = new ArrayList<>();
                for (SongListEntity entity : allList) {
                    MusicListDatas musicList = new MusicListDatas();
                    musicList.setName(entity.getTitle());
                    musicList.setSinger(entity.getAuthor());
                    musicList.setSongId(entity.getSong_id());
                    musicList.setSingerImage(entity.getPic_big());
                    musicList.setFrom(getString(R.string.NET));
                    lists.add(musicList);
                }
                MainActivity.play(lists, 0);
            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ArrayList<MusicListDatas> lists = new ArrayList<>();
                for (SongListEntity entity : allList) {
                    MusicListDatas musicList = new MusicListDatas();
                    musicList.setName(entity.getTitle());
                    musicList.setSinger(entity.getAuthor());
                    musicList.setSongId(entity.getSong_id());
                    musicList.setSingerImage(entity.getPic_big());
                    musicList.setFrom(getString(R.string.NET));
                    lists.add(musicList);
                }
                MainActivity.play(lists, position);
            }
        });

    }

    // 返回按钮
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.collect_back:
                getFragmentManager().popBackStack();
                break;
        }
    }

    private void init(){
        collect_back = (ImageButton) v.findViewById(R.id.collect_back);
        play = (ImageButton) v.findViewById(R.id.collect_play);
        listView = (ListView) v.findViewById(R.id.collect_listView);
        total = (TextView) v.findViewById(R.id.collect_total);
        offLine = (TextView) v.findViewById(R.id.collect_offLine);
        daoSingleton = DaoSingleton.getInstance();
        context = getActivity();
        mController = UMServiceFactory.getUMSocialService(getString(R.string.UMSocialService));
    }

    @Override
    public void onClickToShare(SongListEntity songListEntity, int position) {
        mController.openShare(getActivity(), false);
    }
}
